﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.Samples.ServiceHosting.StorageClient;
using WASM.WebSite.DataEntities;
using System.Data.Services.Client;
using WASM.WebSite.Logic;

namespace WASM.WebSite.Models
{
	public class TableRepository
	{
		TableStorage _tableStorage = null;

		public TableRepository(string storageUri, string accountName, string key)
		{
			_tableStorage = TableStorage.Create(new Uri(storageUri), null, accountName, key);
		}

		public static TableRepository Create(Guid azureStorageId)
		{
			AzureStorage azureStorage = AzureStorageUserSettingsRepository.Single(azureStorageId);
			return new TableRepository(azureStorage.asUrl, azureStorage.asAccountName, azureStorage.asKey);
		}

		public string[] TableList()
		{
			return _tableStorage.ListTables().OrderBy(e => e).ToArray();
		}

		public bool IsTableExist(string tableName)
		{
			return _tableStorage.DoesTableExist(tableName);
		}

		public void TableCreate(string tableName)
		{
			_tableStorage.CreateTable(tableName);
		}

		public void TableDelete(string tableName)
		{
			_tableStorage.DeleteTable(tableName);
		}

		public IEnumerable<TableGenericEntity> Rows(string tableName, int? maxRecordsCount)
		{
			DataServiceQuery<TableGenericEntity> query = GetDataContext().CreateQuery<TableGenericEntity>(tableName);

			if (maxRecordsCount.HasValue)
			{
				query = (DataServiceQuery<TableGenericEntity>)query.Take(maxRecordsCount.Value);
			}

			TableStorageDataServiceQuery<TableGenericEntity> tableStorageQuery = new TableStorageDataServiceQuery<TableGenericEntity>(query);
			IEnumerable<TableGenericEntity> res = tableStorageQuery.Execute();

			return res;
		}

		public TableGenericEntity GetEntity(TableStorageDataServiceContext dc, string tableName, string partitionKey, string rowKey)
		{
				DataServiceQuery<TableGenericEntity> query = (DataServiceQuery<TableGenericEntity>)dc.
				CreateQuery<TableGenericEntity>(tableName).Where(e => e.PartitionKey == partitionKey && e.RowKey == rowKey);

			TableStorageDataServiceQuery<TableGenericEntity> tableStorageQuery = new TableStorageDataServiceQuery<TableGenericEntity>(query);
			TableGenericEntity res = tableStorageQuery.Execute().Single();
			return res;
		}

		public TableGenericEntity GetEntity(string tableName, string partitionKey, string rowKey)
		{
			return GetEntity(GetDataContext(), tableName, partitionKey, rowKey);
		}

		public void EntityDelete(string tableName, string partitionKey, string rowKey)
		{
			TableStorageDataServiceContext dc = GetDataContext();
			TableGenericEntity tableGenericEntity = GetEntity(dc, tableName, partitionKey, rowKey);
			dc.DeleteObject(tableGenericEntity);
			dc.SaveChanges();
		}

		public void EntityCreate(string tableName, TableGenericEntity tableGenericEntity)
		{
			TableStorageDataServiceContext dc = GetDataContext();
			dc.IgnoreMissingProperties = true;
			dc.WritingEntity += new EventHandler<ReadingWritingEntityEventArgs>(TableHelper.OnWritingEntity);

			dc.AddObject(tableName, tableGenericEntity);
			dc.SaveChanges();
		}

		private TableStorageDataServiceContext GetDataContext()
		{
			TableStorageDataServiceContext dc = _tableStorage.GetDataServiceContext();

			dc.IgnoreMissingProperties = true;
			dc.ReadingEntity += new EventHandler<ReadingWritingEntityEventArgs>(TableHelper.OnReadingEntity);

			return dc;
		}
	}
}
